//
//  CustomTabBarViewController.m
//  ViewAnimation
//
//  Created by Billy on 15/5/21.
//  Copyright (c) 2015年 seven. All rights reserved.
//

#import "CustomTabBarViewController.h"
#import "HomePageController.h"
#import "AccountingCircleController.h"
#import "MessageCenterController.h"
#import "UserCenterController.h"

@interface CustomTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation CustomTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self setDelegate:self];

    [self subControllersCreation];
}


#pragma mark - SubControllerCreation

- (void) subControllersCreation {
    
    // 首页
    
    HomePageController *homeVC = [[HomePageController alloc] init];
    [self setSubController:homeVC withTitle:@"首页" andDefautImageName:@"home"
      andSelectedImageName:@"homeSelected"];
    
    // 会计圈
    
    AccountingCircleController *accountingVC = [[AccountingCircleController alloc] init];
    [self setSubController:accountingVC withTitle:@"会计圈" andDefautImageName:@"circle"
      andSelectedImageName:@"circleSelected"];
    
    // 消息
    
    MessageCenterController *messageVC = [[MessageCenterController alloc] init];
    [self setSubController:messageVC withTitle:@"消息" andDefautImageName:@"message"
      andSelectedImageName:@"messageSelected"];
    
    [messageVC.tabBarItem setBadgeValue:@"12"];
    // 个人中心
    
    UserCenterController *userVC = [[UserCenterController alloc] init];
    
    [self setSubController:userVC withTitle:@"个人中心" andDefautImageName:@"user"
      andSelectedImageName:@"userSelected"];
    
}

- (void) setSubController:(UIViewController *) subViewController withTitle:(NSString *) title
                                            andDefautImageName:(NSString *) imageName
                                            andSelectedImageName:(NSString *) selectedImageName{
    
    [subViewController.tabBarItem setTitle:title];
    

    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] <= 7.1) {
        
        [subViewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:selectedImageName] withFinishedUnselectedImage:[UIImage imageNamed:imageName]];
        
    } else {
        
        subViewController.tabBarItem.image = [UIImage imageNamed:imageName];
        subViewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:subViewController];
    
    [self addChildViewController:nav];
}

#pragma mark - TabBar delegate


//可以用来禁用某个item的选择
- (BOOL)tabBarController:(UITabBarController *)tabBarController
                    shouldSelectViewController:(UIViewController *)viewController {
    

    return YES;
    
    
    
}

//用来定位用户选择的item选择
- (void)tabBarController:(UITabBarController *)tabBarController
 didSelectViewController:(UIViewController *)viewController {
  
    NSLog(@"用户点击了第%ld个item!",[tabBarController.viewControllers indexOfObject:viewController]);
    
//    if (SYSTEM_VERSION  < 7.1) {
//        
//        [self.tabBar setSelectedImageTintColor:[UIColor orangeColor]];
//    } else {
//        
//        [self.tabBar setSelectedImageTintColor:[UIColor orangeColor]];
//    }
    
    
    
}


-(BOOL) shouldAutorotate {
    return YES;
}


- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController {
    

    return UIInterfaceOrientationMaskPortrait;

}


- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController {

    return UIInterfaceOrientationPortrait;

}
@end
